package com.ruoyi.help.service.impl;

import com.ruoyi.help.domain.Appointment;
import com.ruoyi.help.mapper.AppointmentMapper;
import com.ruoyi.help.service.IAppointmentService;
import com.ruoyi.help.utils.DateUtil;
import com.ruoyi.help.utils.EnterpriseWeChatRobot;
import com.ruoyi.help.utils.MailUtils;
import com.ruoyi.help.utils.MarketPasswordUtil;
import com.ruoyi.student.domain.Student;
import com.ruoyi.student.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 帮扶预约Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-19
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询帮扶预约
     *
     * @param id 帮扶预约主键
     * @return 帮扶预约
     */
    @Override
    public Appointment selectAppointmentById(String id) {
        if (id.length() > 15) {
            String decode = MarketPasswordUtil.decode(id);
            return appointmentMapper.selectAppointmentById(decode);
        }
        return appointmentMapper.selectAppointmentById(id);
    }

    /**
     * 查询帮扶预约列表
     *
     * @param appointment 帮扶预约
     * @return 帮扶预约
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment) {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增帮扶预约
     *
     * @param appointment 帮扶预约
     * @return 结果
     */
    @Override
    public int insertAppointment(Appointment appointment) {
        Map<String, Student> map = studentMapper.findall().stream().filter(student -> student.getName().equals(appointment.getStuName())).collect(Collectors.toMap(Student::getName, s -> s));
        if(map.get(appointment.getStuName()) == null){
            return -5;
        }

        appointment.setCreationTime(new Date());


        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改帮扶预约
     *
     * @param appointment 帮扶预约
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment) {
        // 学生email
        String email = "3357124907@qq.com";
        if (appointment.getHelpType() == 0) {
            EnterpriseWeChatRobot.sendMan(appointment.getStuName(), "服务评价",
                    " 你好，对于[" + DateUtil.getTime(appointment) +
                            "]的问题是否得到解决 \r\n  请点击：http://localhost/evaluate?code=" +
                            MarketPasswordUtil.encode(appointment.getId()) + "' \r\n" +
                            "对此次服务做出你的评价 [源码时代]"
            );
            MailUtils.sendMail(
                    email,
                    "服务评价",
                    "<h4>   " + appointment.getStuName() +
                            " 你好，对于[" + DateUtil.getTime(appointment) +
                            "]的问题是否得到解决</h4><br>请点击<a href='http://localhost/evaluate?code=" +
                            MarketPasswordUtil.encode(appointment.getId()) + "'" + ">我要评价</a>" +
                            "对此次服务做出你的评价 [源码时代]");
        }
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除帮扶预约
     *
     * @param ids 需要删除的帮扶预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByIds(String[] ids) {
        return appointmentMapper.deleteAppointmentByIds(ids);
    }

    /**
     * 删除帮扶预约信息
     *
     * @param id 帮扶预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentById(String id) {
        return appointmentMapper.deleteAppointmentById(id);
    }
}
